package com.issoft.studentresitor.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import com.issoft.studentresitor.domain.dto.RegistrationDTO;
import com.issoft.studentresitor.domain.pojo.Registration;
import com.issoft.studentresitor.domain.pojo.Result;
import com.issoft.studentresitor.domain.query.RegistrationQuery;
import com.issoft.studentresitor.domain.vo.RegistrationVo;
import com.issoft.studentresitor.service.IRegistrationService;
import com.issoft.studentresitor.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author GJH
 * @since 2025-09-14
 */
@Api(tags = "报到接口")
@Slf4j
@RestController
@RequestMapping("/admin/regis")
public class RegistrationController {
    @Autowired private IRegistrationService regService;
    /**
     * 分页查询
     *
     * @return 报道信息
     */
    @ApiOperation("根据studentId status 分页查询")
    @GetMapping("/queryRegis")
    public Result queryRegis(@RequestParam(required = false) String studentId,
                             @RequestParam(required = false) String status,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "5") Integer pageSize) {
        RegistrationQuery query = new RegistrationQuery();
        query.setStudentId(studentId);
        query.setStatus(status);
        query.setPage(page);
        query.setPageSize(pageSize);
        PageResult<RegistrationVo> result = regService.queryRegisPage(query);
        return Result.success(result);
    }

    /**
     *
     * 根据id批量删除
     * @param ids
     * @return
     */
    @ApiOperation("根据id批量删除")
    @DeleteMapping("/batch")
    public Result batchDelete(@RequestParam List<Integer> ids){
        boolean b = regService.removeByIds(ids);
        return b ? Result.success() : Result.error("批量删除失败");
    }
    /**
     *
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Registration registration = regService.getById(id);
        return registration != null ? Result.success(registration) : Result.error("查询失败");
    }
    /**
     *
     * 新增
     * @param registration
     * @return
     */
    @ApiOperation("新增")
    @PostMapping
    public Result save(@RequestBody Registration registration){
        //插入当前时间
        registration.setRegistrationTime(LocalDateTime.now());
        boolean result = regService.save(registration);
        return result ? Result.success("保存成功") : Result.error("保存失败");
    }
    /**
     * 修改
     */
    @ApiOperation("修改")
    @PutMapping
    public Result update(@RequestBody RegistrationDTO registrationDTO){
        Registration registration = new Registration();
        BeanUtil.copyProperties(registrationDTO,registration);
        boolean result = regService.updateById(registration);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }
    /**
     * 根据id修改报到状态
     */
    @ApiOperation("根据id修改报到状态")
    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestParam Integer id,@RequestParam String status){
        Registration registration = new Registration();
        registration.setRegistrationId(id);
        registration.setStatus(status);
        boolean result = regService.updateById(registration);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }
}
